#include <pic.h>
#include <stdio.h>
#include "timing.h"


void delayMs(unsigned int x) { 
	unsigned long start_millis = 0;
	unsigned long end_millis = 0;

	//limited to 999 milliseconds
	if(x > 999) 
	{
		x = 999;
	}

	start_millis = newmillisecond;  
	end_millis = start_millis + x;

	//end_millis needs to be limited to a full second
	if (end_millis > 999)
	{
		end_millis = end_millis - 999;
	}

	//if the counter wraps around then we count until the counter is less than the start
	// or in other words until we overflow and then count up until the counter is greater than 
	// end otherwise a normal count
	if (end_millis < start_millis)
	{
		//adjustment for handling wrapping around
		while(newmillisecond > start_millis)
		{
			continue;
		} 		
	}
	
	//gotta do this whether there is a wraparound or not
	while(newmillisecond < end_millis)
	{
		continue;
	}					
}

void delaySec(unsigned char x){
	unsigned long end_secs = 0;
	
	//limited to 59 seconds
	if(x > 59) 
	{
		x = 59;
	}

	end_secs = countdownseconds - x;
	
	while(countdownseconds > end_secs)
	{
		continue;
	}		
}

void genFreqSecs(unsigned int afrq, unsigned int adur) {
	unsigned int frq_cnt = 0;
	unsigned long conv_frq = 0;

	if (afrq > 14000) afrq = 14000;
	conv_frq = 14000 / afrq;
	adur = countdownseconds - adur;

	while(countdownseconds > adur) {
		frq_cnt = 0;

		while(frq_cnt < conv_frq){
			frq_cnt++;	
#if ENABLE_SOUND == 1 
			SPKR = 1;
#endif
		}
			
	    frq_cnt = 0;
		while(frq_cnt < conv_frq){
			frq_cnt++;	
#if ENABLE_SOUND == 1 
			SPKR = 0;
#endif
		}
	}		
}


static void interrupt
isr(void)			// Here be interrupt function - the name is unimportant.
{
	// ********** setup for timer 0
	if((T0IE) && (T0IF)) 
	{	
		//frequency is 976.5625hz or 1 pulse every 1.02msec
		
		newmillisecond++;  //a millisecond has accumulated
					
		if(newmillisecond > 999)
		{
			newSecond++;	// a second has accumulated
			countdownseconds--;  //as long as this is larger than game_time_in_secs then this is used for delays
		
			if (newSecond > 59)
			{
				newSecond = 0;
			}

			if(gameplaying)
			{
				game_time_in_secs--;
			}
			newmillisecond = 0; //reset the milliseconds
		}
		
		T0IF = 0;			// clear the interrupt flag
	}
}

void init_interrupts(void){

	//	Timer 0 interrupt enable 
	//  clock frequency/4/prescaler/max count = interrupt frequency in hertz
	//  example:  4,000,000/4/4/256 = 976.5625hz
	//  duration between interrupts:  1/hz or in this case 1/976.562hz = 0.001024 or 1.02msec
	//  see page 232 of "Designing Embedded Systems with PIC Microcontrollers" by Tim Wilmshurst

	T0CS = 0; 	 // timer0 clock source set to internal clock
	PSA = 0; 	 // prescaler is assigned to timer0
	PS2 = 0;	 //prescaler set to 1:4
	PS1 = 0;
	PS0 = 1;
	
	PEIE = 1;	 // gotta enable peripheral interrupts
	GIE =  1; 	 // gotta enable global interrupts

	T0IE = 1;    // timer 0 interrupt enable
}
